package com.thinkit.bigdata.web.service.impl.bus;

import com.thinkit.bigdata.web.dao.bus.BusModelMapper;
import com.thinkit.bigdata.web.dao.bus.BusModelRuleMapper;
import com.thinkit.bigdata.web.model.bus.BusModel;
import com.thinkit.bigdata.web.model.bus.BusModelRule;
import com.thinkit.bigdata.web.model.bus.BusModelRuleExample;
import com.thinkit.bigdata.web.service.bus.BusModelRuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * Created by hccl on 2018/9/10.
 *
 * modify by lichangyuan on 2018年10月10日.
 */
@Service
public class BusModelRuleServiceImpl implements BusModelRuleService {

    @Resource
    private BusModelRuleMapper busModelRuleMapper;

    @Resource
    private BusModelMapper busModelMapper;

    @Override
    public int countByExample(BusModelRuleExample example) {
        return busModelRuleMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(BusModelRuleExample example) {
        return busModelRuleMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return busModelRuleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(BusModelRule record) {
        return busModelRuleMapper.insert(record);
    }

    @Override
    public int insertSelective(BusModelRule record) {
        return busModelRuleMapper.insertSelective(record);
    }

    @Override
    public List<BusModelRule> selectByExample(BusModelRuleExample example) {
        return busModelRuleMapper.selectByExample(example);
    }

    @Override
    public List<BusModelRule> getRuleMesByModelId(Long modelId) {
        BusModelRuleExample example = new BusModelRuleExample();
        example.createCriteria().andModelidEqualTo(modelId);
        return busModelRuleMapper.selectByExample(example);
    }

    @Override
    public BusModelRule selectByPrimaryKey(Long id) {
        return busModelRuleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(BusModelRule record, BusModelRuleExample example) {
        return busModelRuleMapper.updateByExampleSelective(record,example);
    }

    @Override
    public int updateByExample(BusModelRule record, BusModelRuleExample example) {
        return busModelRuleMapper.updateByExample(record,example);
    }

    @Override
    public int updateByPrimaryKeySelective(BusModelRule record) {
        return busModelRuleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(BusModelRule record) {
        return busModelRuleMapper.updateByPrimaryKey(record);
    }


    /**
     * 删除 已经删除的规则与下线模型的关系
     * @param ruleIds
     * @return
     */
    @Override
    public void delete_offline_model_rule(List<Long> ruleIds ){
        BusModelRuleExample example = new BusModelRuleExample();
        example.createCriteria().andRuleidIn(ruleIds);
        List<BusModelRule> ruleList = busModelRuleMapper.selectByExample(example);//根据ruleId去查询
        for (BusModelRule rule : ruleList) {
            BusModel model = busModelMapper.selectByPrimaryKey(rule.getModelid());//根据modelId查询
            if (model.getPoststatus() == 0) { //模型是否下线
                busModelRuleMapper.deleteByPrimaryKey(rule.getId());//根据id删除规则
            }
        }
    }

}
